package com.ihunanren.common.auth;

import static com.google.common.base.Preconditions.checkArgument;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * @author kalman03
 */
public class SHA1 {
	
	public static String encrypt(String ... args){
		checkArgument(args != null && args.length >0);
		try {
			Arrays.sort(args);
			StringBuilder sb = new StringBuilder();
			for(String str : args){
				sb.append(str);
			}
			MessageDigest md = MessageDigest.getInstance("SHA1");
			return byte2hex(md.digest(sb.toString().getBytes("utf-8")));
		} catch (Exception e) {
			throw new RuntimeException("SHA1 encrypt error."+e.getMessage());
		}
	}
	
	/**
	 * 二行制转字符串
	 */
	private static String byte2hex(byte[] b) {
		StringBuilder hs = new StringBuilder();
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs.append("0").append(stmp);
			else
				hs.append(stmp);
		}
		return hs.toString().toUpperCase();
	}
	
	public static void main(String[] args) {
		//0fa874a16dec27791cac2bde34154f3e949d396c
		System.out.println(SHA1.encrypt("weixin","1364538841","1364353840"));
		
	}
}
